package com.fare.taluo;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.dayatang.querychannel.support.Page;

/**
 * 类    名：PageComponent.java<br />
 *   
 * 功能描述：分页组件 for：struts2+freemarker	<br />
 *  
 * 创建日期：2011-11-7上午08:50:52  <br />   
 * 
 * 版本信息：v 1.0<br />
 * 
 * 版权信息：Copyright (c) 2011 Csair All Rights Reserved<br />
 * 
 * 作    者：<a href="mailto:jiangwei@foreveross.com">vakin jiang</a><br />
 * 
 * 修改记录： <br />
 * 修 改 者    修改日期     文件版本   修改说明	
 */
public class PageComponent implements Serializable{
    private static final long serialVersionUID = 1L;

    public static int DEFAULT_PAGE_SIZE = 10;
    
    private int pageSize = DEFAULT_PAGE_SIZE; // 每页的记录数

    private long totalCount; // 总记录数
    private long currentPageNo;//当前页码

    private PageComponent() {
        super();
    }
    /**
     * @param pageSize
     * @param start
     * @param totalCount
     * @param currentPageNo
     */
    private PageComponent(int pageSize, long totalCount, long currentPageNo) {
        super();
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.currentPageNo = currentPageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public void setCurrentPageNo(long currentPageNo) {
        this.currentPageNo = currentPageNo;
    }
    public long getTotalCount() {
        return totalCount;
    }

    public long getCurrentPageNo() {
        return currentPageNo;
    }
    
    public long getStartPoint() {
        return (currentPageNo - 1) * pageSize + 1;
    }
    
    public long getEndPoint() {
        long end = currentPageNo* pageSize;
        return end > getTotalCount() ? getTotalCount() : end;
    }

    public boolean getHasNextPage() {
        return this.getCurrentPageNo() < this.getTotalPageCount() ? true :false;
    }

    public boolean getHasPreviousPage() {
        return getCurrentPageNo() > 1 ? true :false;
    }
    
    public String getNextPageUrl(){
        return getPageUrl((currentPageNo+1)+"");
    }
    
    public String getLastPageUrl(){
        return getPageUrl(getTotalPageCount()+"");
    }
    
    public String getFirstPageUrl(){
        return getPageUrl("1");
    }
    
    public String getPrePageUrl(){
        return getPageUrl((currentPageNo - 1)+"");
    }
    
    public String getNoPageNoPageUrl(){
        return getPageUrl("");
    }
    
    public long getTotalPageCount() {
        if (totalCount % pageSize == 0)
            return totalCount / pageSize;
        else
            return totalCount / pageSize + 1;
    }
    
    public static <T> PageComponent getInstance(Page<T> page){
        PageComponent instance = new PageComponent(page.getPageSize(), page.getTotalCount(), page.getCurrentPageNo());
        return instance;
    }
    
    public String getPageUrl(String pageNo){
        String queryString = ServletActionContext.getRequest().getQueryString();
        if(StringUtils.isEmpty(queryString)){
            queryString = "?currentPageNo="+(pageNo);
        }else{
            //delayFltType=schedule&currentPageNo=2
            if(queryString.indexOf("&currentPageNo")>=0){
                queryString = queryString.replaceAll("(&currentPageNo=)[^&]{1,}", "");
            }else if(queryString.indexOf("currentPageNo")>=0){
                queryString = queryString.replaceAll("(currentPageNo=)[^&]{1,}", "");
            }
            queryString = "?"+queryString + "&currentPageNo="+(pageNo);
        }
        return queryString;
    }
}
